<?php

namespace App\model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use DB;
use App\model\mongodb;

class post extends Model
{
    //

    private $doc;

    private $author;

    private $author_id;

    private $avatar;

    private $title;

    private $brief;

    private $content;

    private $star;

    private $top;

    private $img;

    public function __construct(){
        date_default_timezone_set('PRC');
    	// $this->database = mongodb::connectionMongodb('post');
        $num = func_num_args();   //获得参数个数
        $args = func_get_args();   //获得参数列表数组
        switch($num){
            case 0:
                break;
            case 1:
                $this->constructWithDoc($args[0]);
                break;
            default:
                throw new Exception();
        }
    }

    public function constructWithDoc($doc){
    	$this->database = mongodb::connectionMongodb('post', $doc);
        $this->doc = $doc;
    	$thePost = $this->database->where(['_id' => 1])->get()->toArray();
    	foreach ($thePost[0] as $key => $value) {
            if ($key == 'top') {
                continue;
            }
    		$this->$key = $thePost[0][$key];
    	}
    	return $this;
    }

    public function newPost($post){
    	if (!$this->judgementOfPost($post)) {
    		return 0;
    	}
    	foreach ($post as $key => $value) {
    		if ($key == '_id') {
    			continue;
    		}
    		$this->$key == $value;
    	}
    	$time = time();
    	$this->database = mongodb::connectionMongodb('post', $post['title'].$time);
        $this->doc = $post['title'].$time;
        $fastTable['doc'] = $post['title'].$time;
        $fastTable['title'] = $post['title'];
        $fastTable['brief'] = $post['brief'];
        $fastTable['status'] = 'processed';
        $fastTable['commit_time'] = explode(' ', microtime())[1];
        $fastTable['microtime'] = explode('.', explode(' ', microtime())[0])[1];
        $fastTable['top'] = 0;
        $fastTable['img'] = json_encode($post['img']);
        $fastTable['author'] = $post['author'];
        $fastTable['avatar'] = $post['avatar'];
        $fastTable['author_id'] = $post['author_id'];
        $post['img'] = json_encode($post['img']);
        $post['_id'] = 1;
        var_dump($post);
    	$success = $this->database->insert($post) ? 1 : 0;
        if ($success != 1) {
            return 0;
        }
        $success = DB::table('sots_post')->insert($fastTable);
    	return $success;
    }

    public function changePost($post){
    	foreach ($post as $key => $value) {
    		if ($key == '_id') {
    			continue;
    		}
    		$this->$key == $value;
    	}
    }

    public function savePost(){
        $this->database = mongodb::connectionMongodb('post', $this->doc);
    	$post['doc'] = $this->doc;
        $post['title'] = $this->title;
        $post['content'] = $this->content;
        $post['brief'] = $this->brief;
        $post['star'] = $this->star;
        $post['top'] = $this->top;
    	if (count($this->database->where(['_id' => 1])->get())) {
    		$success = $this->database->insert($post) ? 1 : 0;
    	}else{
    		$success = $this->database->where(['_id' => 1])->update($post) ? 1 : 0;
    	}
    	return $success;
    }

    public function getPost(){
    	$post['doc'] = $this->doc;
        $post['title'] = $this->title;
        $post['content'] = $this->content;
        $post['brief'] = $this->brief;
        $post['star'] = $this->star;
        $post['top'] = $this->top;
        $post['img'] = $this->img;
        $post['author'] = $this->author;
        $post['author_id'] = $this->author_id;
        $post['avatar'] = $this->avatar;
    	return $post;
    }

    public function getPostAppend(){
        $this->database = mongodb::connectionMongodb('post', $this->doc);
        $thePostAppend = $this->database->where('_id', '!=', 1)->get()->toArray();
        foreach ($thePostAppend as $key => $value) {
            foreach ($value['_id'] as $key2 => $value2) {
                $thePostAppend[$key]['_id'] = $value2;
            }
        }
        return $thePostAppend;
    }

    public function appendThePost($append){
        $this->database = mongodb::connectionMongodb('post', $this->doc);
        if (!$this->judgementOfAppend($append)) {
            return 0;
        }
        $append['star'] = 0;
        $success = $this->database->insert($append) ? 1 : 0;
        return $success;
    }

    private function judgementOfPost($keys){
    	$parameter = array('title', 'brief', 'content');
    	foreach ($parameter as $key => $value) {
    		if (!isset($keys[$value])) {
    			return 0;
    		}
    	}
    	foreach ($keys as $key => $value) {
    		if (in_array($value, $parameter)) {
    			return 0;
    		}
    	}
    	return 1;
    }

    private function judgementOfAppend($keys){
        $parameter = array('father', 'content');
        foreach ($parameter as $key => $value) {
            if (!isset($keys[$value])) {
                return 0;
            }
        }
        foreach ($keys as $key => $value) {
            if (in_array($value, $parameter)) {
                return 0;
            }
        }
        return 1;
    }

}
